<!DOCTYPE html>
<html lang="en" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/layui/css/layui.css">
    <link rel="stylesheet" href="/css/custom.form.css">
</head>
<body>
<div class="panel panel-default operation" hidden>
    <div class="panel-heading title"></div>
    <div class="layui-card-body">
        <form class="layui-form " action="" lay-filter="info" style="width: 700px;margin-top: 10px">
            <input name="id" hidden/>f
            <div class="layui-form-item">
                <label class="layui-form-label">标题</label>
                <div class="layui-input-block">
                    <input type="title" name="title" placeholder="请输入标题" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">类型</label>
                <div class="layui-input-block">
                    <select id="type" name="type" lay-filter="select-type" th:with="type=${@sysDictService.getType('content_type')}">
                        <option value="0">请选择</option>
                        <option th:each="dict : ${type}" th:text="${dict.label}" th:value="${dict.value}"></option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">内容</label>
                <div class="layui-input-block">
                    <textarea id="content" name=content style="display: none;"></textarea>
                </div>

            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button type="submit" class="layui-btn" lay-submit="" lay-filter="submit">保存</button>
                    <button class="layui-btn layui-btn-primary" id="btn_cancel">返回</button>
                </div>
            </div>
        </form>
    </div>
</div>

<div class="table_div">
    <div id="searchParam" shiro:hasPermission="sysOrder:list">
        <div class="layui-form-item">
            <div class="layui-input-inline">
                <input type="text" id="title" class="layui-input" autocomplete="off" placeholder="请输入标题">
            </div>

            <div class="layui-input-inline ">
                <button class="layui-btn" onclick="search()">查询</button>
            </div>
        </div>

    </div>
    <table class="layui-table" id="showTable" lay-filter="showTable"></table>
</div>
<script type="text/html" id="toolbar">
    <div class="layui-btn-container">
<!--        <label class="layui-form-label">批量导入</label>-->
<!--        <input type="file" id="ei" class="layui-btn layui-btn-sm" lay-event="excel_import" shiro:hasPermission="sysOrder:add">批量导入</input>-->
        <button type="button" class="layui-btn layui-btn-normal" lay-event="excel_import" id="importData">导入订单文件</button>
        <button class="layui-btn layui-btn-sm" lay-event="add" shiro:hasPermission="sysOrder:add">添加</button>
        <button class="layui-btn layui-btn-sm" lay-event="batchDeleted" shiro:hasPermission="sysOrder:delete">删除
        </button>
    </div>
</script>
<script type="text/html" id="tool">
    <a class="layui-btn layui-btn-xs" lay-event="preview" shiro:hasPermission="sysOrder:list">预览</a>
    <a class="layui-btn layui-btn-xs" lay-event="edit" shiro:hasPermission="sysOrder:update">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del" shiro:hasPermission="sysOrder:delete">删除</a>
</script>

</body>
</html>
<script src="/layui/layui.all.js"></script>
<script src="/js/core.util.js"></script>
<script>
    //文章类型字典数据
    var datas = "[[${@sysDictService.getType('content_type')}]]".replace(/&quot;/g,"\"");
    //获取token
    var token = CoreUtil.getData("access_token");
    //地址栏转义token中的#号
    var tokenQuery = token.replace("#", "%23");
    var tableIns1;
    var table = layui.table;
    var form = layui.form;
    var layer = layui.layer;
    var $ = jQuery = layui.jquery;
    var laydate = layui.laydate;
    var layedit = layui.layedit;
    var upload = layui.upload;

    layui.use(['table', 'layer', 'laydate', 'layedit' , 'upload'], function () {
        //图片
        layedit.set({
            uploadImage: {
                url: '/sysFiles/upload?authorization=' + tokenQuery //接口url
                , type: 'post' //默认post
            }
        });
        //建立富文本编辑器
        var editIndex = layedit.build('content');
        //加载table
        tableIns1 = table.render({
            elem: '#showTable'
            , contentType: 'application/json'
            , headers: {"authorization": token}
            , page: true //开启分页
            , url: '/sysOrder/listByPage' //数据接口
            , method: 'POST'
            , parseData: function (res) { //将原始数据解析成 table 组件所规定的数据
                console.log("订单解.....:::");
                console.log(res.data.records);

                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": CoreUtil.isEmpty(res.data) ? 0 : res.data.total, //解析数据长度
                    "data": CoreUtil.isEmpty(res.data) ? null : res.data.records //解析数据列表

                }
            }
            , cols: [
                [
                    {type: 'checkbox', fixed: 'left'},
                    // {field: 'id', title: 'id', sort: true},
                    {field: 'purchaseId', title: '采购单号'},
                    {field: 'orderId', title: '商品编号'},
                    {field: 'eName', title: '商品名称'},
                    {field: 'ePrice', title: '销售价格', sort: true},
                    {field: 'eNumber', title: '销售数量', sort: true},
                    {field: 'eTotalPrice', title: '销售总金额', sort: true},
                    {field: 'jdPerson', title: '采销人员'},
                    {field: 'createTime', title: '创建时间', sort: true},

                    {width: 200, toolbar: "#tool", title: '操作'}
                ]
            ]
            , toolbar: '#toolbar'
        });

        //表头工具
        table.on('toolbar(showTable)', function (obj) {
            switch (obj.event) {
                case 'batchDeleted':
                    var checkStatus = table.checkStatus(obj.config.id);
                    var data = checkStatus.data;
                    if (data.length == 0) {
                        layer.msg("请选择要批量删除的列");
                    } else {
                        var ids = [];
                        $(data).each(function (index, item) {
                            ids.push(item.id);
                        });
                        tipDialog(ids);
                    }
                    break;
                case 'add':
                    $(".table_div").hide();
                    $(".operation").show();
                    $(".title").html("新增");
                    $(".operation input[name=id]").val("");
                    $(".operation input[name=title]").val("");
                    layedit.setContent(editIndex, "");
                    //渲染select 默认选中第一个
                    $("#type option:first").prop("selected", 'selected');
                    form.render();
                    break;

                case 'excel_import':
                    break;
            };
        });
        //列操作
        table.on('tool(showTable)', function (obj) {
            var data = obj.data;
            switch (obj.event) {
                case 'del':
                    var ids = [];
                    ids.push(data.id);
                    tipDialog(ids);
                    break;
                case 'edit':
                    $(".table_div").hide();
                    $(".operation").show();
                    $(".title").html("编辑");
                    $(".operation input[name=id]").val(data.id);
                    $(".operation input[name=title]").val(data.title);
                    layedit.setContent(editIndex, data.content);
                    //渲染select 默认选中
                    if (!CoreUtil.isEmpty(data.type)) {
                        $("#type").find('option[value="'+data.type+'"]').prop('selected',true);
                        form.render();
                    }
                    break;
                case 'preview':
                    layer.open({
                        type: 1,
                        skin: 'layui-layer-demo', //样式类名
                        title: "预览",
                        shadeClose: true, //开启遮罩关闭
                        area: ['900px', '500px'], //宽高
                        shade: 0.6, //遮罩透明度
                        maxmin: true, //允许全屏最小化
                        anim: 1, //0-6的动画形式，-1不开启
                        content: data.content, //捕获的元素，注意：最好该指定的元素要存放在body最外层，否则可能被其它的相对元素所影响
                        cancel: function () {
                        }
                    });
                    break;
            }
        });

        //删除
        var tipDialog = function (ids) {
            layer.open({
                content: "确定要删除么?",
                yes: function (index, layero) {
                    layer.close(index); //如果设定了yes回调，需进行手工关闭
                    CoreUtil.sendDelete("/sysOrder/delete", ids, function (res) {
                        layer.msg(res.msg, {time: 1000}, function () {
                            search();
                        });
                    })
                }
            });
        };

        //返回
        $("#btn_cancel").click(function () {
            $(".table_div").show();
            $(".operation").hide();
            return false;
        });

        var upload1 =  upload.render({
            elem: "#importData",   //导入id
            url: "/sysOrder/import?authorization=" + tokenQuery,
            accept: "file",
            method : "post",
            size: 1024 * 2,
            /* 允许上传文件的后缀 */
            exts: 'xls|xlsx|xlsm|xlt|xltx|xltm',
            progress: function(n, elem, res, index){
                let percent = n + '%'; //获取进度百分比
                // element.progress('demo', percent); //可配合 layui 进度条元素使用
                console.log("elem 触发元素:"+elem); //得到当前触发的元素 DOM 对象。可通过该元素定义的属性值匹配到对应的进度条。
                console.log("res 响应:"+res); //得到 progress 响应信息
                console.log("index: 文件索引:"+index); //得到当前上传文件的索引，多文件上传时的进度条控制，如：
                console.log("进度: "+percent);
                // element.progress('demo-'+ index, n + '%'); //进度条
                // element.progress('demo-'+ index, n + '%'); //进度条
            },
            /* 上传完毕回调 */
            done: function (res) {
                console.log(res)
                // if(res.code==0){
                //     // iconValue=6;
                // }else if(res.code==500){
                //     // iconValue=5;
                // }
                layer.msg(res.msg, {icon: 3,time: 2000},function(){
                    window.location.reload();
                });
            },
            error: function(){
                console.log("==========uploadExcel=====error=======")
                //请求异常回调
                alert("服务器出小差啦，请稍后再试");
            }
        });

        //监听保存
        form.on('submit(submit)', function (data) {
            data.field.content = layedit.getContent(editIndex);
            if (data.field.id === undefined || data.field.id === null || data.field.id === "") {
                CoreUtil.sendPost("/sysOrder/add", data.field, function (res) {
                    $(".table_div").show();
                    $(".operation").hide();
                    search();
                })
            } else {
                CoreUtil.sendPut("/sysOrder/update", data.field, function (res) {
                    $(".table_div").show();
                    $(".operation").hide();
                    search();
                })
            }

            return false;
        });
    });

    function uploadExcel(){
        console.log("==========uploadExcel============")

        upload.render({
            elem: "#importData",   //导入id
            url: "sysOrder/import",
            accept: "file",
            method : "post",
            size: 1024 * 2,
            /* 允许上传文件的后缀 */
            exts: 'xls|xlsx|xlsm|xlt|xltx|xltm',
            progress: function(n, elem, res, index){
                let percent = n + '%'; //获取进度百分比
                // element.progress('demo', percent); //可配合 layui 进度条元素使用
                console.log(elem); //得到当前触发的元素 DOM 对象。可通过该元素定义的属性值匹配到对应的进度条。
                console.log(res); //得到 progress 响应信息
                console.log(index); //得到当前上传文件的索引，多文件上传时的进度条控制，如：
                console.log(percent);
                // element.progress('demo-'+ index, n + '%'); //进度条
                // element.progress('demo-'+ index, n + '%'); //进度条
            },
            /* 上传完毕回调 */
            done: function (res) {
                console.log(res)
                // if(res.code==0){
                //     // iconValue=6;
                // }else if(res.code==500){
                //     // iconValue=5;
                // }
                layer.msg(res.msg, {icon: iconValue,time: 2000},function(){
                    window.location.reload();
                });
            },
            error: function(){
                console.log("==========uploadExcel=====error=======")
                //请求异常回调
                alert("服务器出小差啦，请稍后再试");
            }
        });

    }


    //执行查询
    function search() {
        //这里以搜索为例
        tableIns1.reload({
            where: { //设定异步数据接口的额外参数，任意设
                title: $("#title").val()
            }
            , page: {
                curr: 1 //重新从第 1 页开始
            }
        });
    };
</script>